from app import create_app
import logging


def run(self):
    for generation in range(self.phenotype_limit):
        self.cal_fitness()
        self.select_parents()
        self.crossover_and_mutation()

        if self.temperature > 1:
            self.temperature *= self.cooling_rate

        logging.info(
            f"Generation {generation + 1}, Best Fitness: {max(order.fitness for order in self.orders)}, Temperature: {self.temperature:.2f}")
        # 添加新的日志记录
        unassigned_slots = self.count_unassigned_slots()
        conflicts = self.count_scheduling_conflicts()
        logging.info(f"Unassigned slots: {unassigned_slots}, Scheduling conflicts: {conflicts}")


def count_unassigned_slots(self):
    return sum(1 for order in self.orders for gene in order.order if gene.caregiver == self.default_caregiver)


def count_scheduling_conflicts(self):
    conflicts = 0
    for order in self.orders:
        caregiver_schedules = {}
        for gene in order.order:
            if gene.caregiver not in caregiver_schedules:
                caregiver_schedules[gene.caregiver] = set()
            time_slot = int(gene.schedule.start_time * 2)
            if time_slot in caregiver_schedules[gene.caregiver]:
                conflicts += 1
            caregiver_schedules[gene.caregiver].add(time_slot)
    return conflicts


app = create_app()

if __name__ == '__main__':
    print("** 首页接口：http://127.0.0.1:10338/dashboard **")
    app.run(host='0.0.0.0', port=10338, debug=True)
